<?php

namespace app\common\model;
use think\Model;

class UserAddressModel extends Model
{
    protected $name = 'user_address';

    /**
     * 获取地址列表
     *
     * @param $user_id int 会员ID
     * @param $page int 页码
     * @param $limit int 每页数据
     */
    public function getPageList(int $user_id, int $page, int $limit): array
    {
        try {
            $field = ['id','user_id','province','city','area','detail','name','mobile','is_default'];
            $where = [
                'user_id' => $user_id,
            ];
            $list = $this
                ->field($field)
                ->where($where)
                ->order('is_default desc')
                ->paginate($limit, false, ['page' => $page]);
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $list
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }


    /**
     * 获取地址详情
     *
     * @param $address_id int 地址ID
     * @param $user_id int 用户ID
     */
    public function detail($params, int $user_id): array
    {
        extract($params);

        try {
            $data = $this->get($address_id);
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $data
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }

    /**
     * 获取默认地址
     * @param $user_id int 用户ID
    */
    public function UserDefaultAddress($user_id)
    {
        try {
            $field = ['id','user_id','province','city','area','detail','name','mobile','is_default'];
            $data = $this
                ->field($field)
                ->where(['is_default' => 1, 'user_id' => $user_id])
                ->find();
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $data
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }


    /**
     * 添加地址
     * @param $user_id int 用户ID
     * @param $province string 省
     * @param $city string 市
     * @param $area string 区
     * @param $detail string 详细地址
     * @param $name string 姓名
     * @param $mobile string 手机号
     * @param $is_default int 是否默认:0=不是,1=是
     */
    public function addAddress($params, $user_id)
    {
        extract($params);

        try {
            if ($is_default == 1) {
                $this->where(['user_id' => $user_id])->update(['is_default' => 0]);
            }
            $this->create([
                'user_id'    => $user_id,
                'province'   => $province,
                'city'       => $city,
                'area'       => $area,
                'detail'     => $detail,
                'name'       => $name,
                'mobile'     => $mobile,
                'createtime' => time(),
                'is_default' => $is_default,
            ]);
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => []
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }

    /**
     * 修改地址
     * @param $user_id int 用户ID
     * @param $address_id int 地址ID
     * @param $province string 省
     * @param $city string 市
     * @param $area string 区
     * @param $detail string 详细地址
     * @param $name string 姓名
     * @param $mobile string 手机号
     * @param $is_default int 是否默认:0=不是,1=是
     */
    public function editAddress($params, $user_id)
    {
        extract($params);

        try {
            if ($is_default == 1) {
                $this->where(['user_id' => $user_id,'id' => ['<>', $address_id]])->update(['is_default' => 0]);
            }
            $this->save([
                'user_id' => $user_id,
                'province' => $province,
                'city' => $city,
                'area' => $area,
                'detail' => $detail,
                'name' => $name,
                'mobile' => $mobile,
                'createtime' => time(),
                'is_default' => $is_default,
            ],['id' => $address_id]);
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => []
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }

    /**
     * 删除地址
     * @param $user_id int 用户ID
     * @param $address_id int 地址ID
     */
    public function deleteAddress($params, $user_id)
    {
        extract($params);

        try {
            $this->where(['id' => $address_id])->delete();
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => []
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }

    /**
     * 设为默认地址
     * @param $user_id int 用户ID
     * @param $address_id int 地址ID
     */
    public function addDefault($params, $user_id)
    {
        extract($params);

        try {
            $this->where(['user_id' => $user_id])->update(['is_default' => 0]);
            $this->where(['user_id' => $user_id, 'id' => $address_id])->update(['is_default' => 1]);
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => []
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }



    /**
     * 获取默认地址信息
    */
    public static function theDefault($user_id, $user_address_id)
    {
        $where = [
            'user_id' => $user_id
        ];
        if (!$user_address_id) {
            $where['is_default'] = 1;
        }else{
            $where['id'] = $user_address_id;
        }
        return self::field('id,name,mobile,province,city,area,detail')
            ->where($where)
            ->find();
    }


}